<?php
require 'config/config.php';
// ----------------------------------------------
//  Instantiate MAIN CORE class
// ----------------------------------------------    
require 'core/main.class.php';
$MAIN = new main(); 
require_once("core/sessions.class.php");
$SESS = new Session();
// var_dump($SESSION);
require_once("core/promotion.class.php");
require_once("core/promotionalItem.class.php");	
require_once("display/adminposdisplay.class.php");
require_once("functions/stringFunctions.php");
$SFUNCTION = new StringFunctions();
$promotion = new Promotion();
$ADMINDISPLAY = new Adminposdisplay();

$availableitemspos = array();

$chosenItems = array();




$result = $ADMINDISPLAY->getCurrentPos();
	while ($array = mysql_fetch_array($result) ){
		$availableitemspos[ $SFUNCTION->formFormatted($array['itemName']) ] =  array();
		$availableitemspos[ $SFUNCTION->formFormatted($array['itemName']) ]['type'] = $SFUNCTION->formFormatted($array['type']);
	}





foreach ($_POST as $key => $val) {
	if (array_key_exists($key,$availableitemspos)){
	$promoItem = new PromotionalItem();
	$promoItem->setItemName($SFUNCTION->camelCase($key));
	$promoItem->setItemCost($_POST['price_'.$key]);
	
	if (isset($_POST['free_'.$key])){
		$promoItem->setFreePOS(TRUE);
	}else{
		$promoItem->setFreePOS(FALSE);
	}
	
	if (!empty($_POST['extra_'.$key])){
		$promoItem->setextraInfo($_POST['extra_'.$key]);
	}else{
		$promoItem->setextraInfo("");
	}
	
	$type = $availableitemspos[$key]['type'];
	$promoItem->setTypeOfPos($SFUNCTION->camelCase($type));
	
		array_push($chosenItems,$promoItem);
	}
}


 

$promotion->startDate = reverseDates($_POST['startDate']);
$promotion->endDate = reverseDates($_POST['endDate']);
$promotion->targetDate = reverseDates($_POST['targetDate']);

$promotion->setPromotionName($_POST['nameOfPromotion']);
$promotion->setSector($_POST['sector']);
$promotion->setCategory($_POST['category']);
$promotion->addPromotionalItems($chosenItems);

// SELECT OF ADD NEW SUBCATEGORY
$subCategory = $_POST['subcategories'];


function reverseDates($date){
	$revdate = explode("-", $date);
	return ($revdate[2]."-".$revdate[1]."-".$revdate[0]);
}
//echo $subCategory;







// Determine what extras have been selected and write 
// the code to allow the user to specify the parameters

$html = "";
$html .= "<div id=\"extraFunctionsBlock\" class=\"form-container\">\n";
$html .= "<h2 class=\"adminHeader\">Extras for ".$promotion->getPromotionName()."</h2>\n";
$html .= "<form method=\"post\" action=\"addpromos.php\" enctype=\"multipart/form-data\">";
if($_POST['dontChangeImages'] == true){

$html .= "<input name=\"editPromo\" type=\"hidden\" id=\"editPromo\" value=\"true\" />";
$html .= "<input type=\"hidden\" id=\"promoId\" name=\"promoId\" value=\"".$_POST['promoId']."\">\n";


}
// this comes from the edit promotions
if($_POST['dontChangeImages'] == false){


if (isset($_POST['numberOfImages'])){

	if( $_POST['numberOfImages'] > 0 ){

	$numberOfImages = $_POST['numberOfImages'];
		if($numberOfImages > 0){
			$html .= "<fieldset><legend>Images</legend>\n";
				for($i=1; $i<=$numberOfImages; $i++){
					$html .= "<label>Images $i</label><input type=file name='images[]' class='bginput'><br/>\n";
				}
			$html .= "</fieldset>";
		}		
	}
}

}

$extras = "";

switch ( $subCategory )
{
    case "new":
    	$html .= "<fieldset><legend>Write Sub Category Title</legend>\n";
        $html .= "<label class=\"promotionalNameLabel\">Sub Category</label><input name=\"subCategory\" type=\"text\" id=\"subCategory\" size=\"20\" />\n";
		$html .= "</fieldset>";
        break;
    case "none":
        
        break;
    default:
    	$html .= "<input type=\"hidden\" id=\"subCategoryId\" name=\"subCategoryId\" value=\"".$subCategory."\">";
        $promotion->setSubCategory($_POST['subCategory']);
        break;
}

if (isset($_POST['writeNumberOfItems'])){
	$html .= "<fieldset><legend>Write Number Of Items</legend>\n";
	$html .= "<label for=\"writeNumberOfItemsVal1\">Enter Start Amount</label><input type=\"text\" id=\"writeNumberOfItemsVal1\" name=\"writeNumberOfItemsVal1\" size=\"2\" /><br/>\n";
	$html .= "<label for=\"writeNumberOfItemsVal2\">Enter End Amount</label><input type=\"text\" id=\"writeNumberOfItemsVal2\" name=\"writeNumberOfItemsVal1\" size=\"2\" />\n";
	$html .= "</fieldset>";
	}

if (isset($_POST['foodItems'])){
	$html .= "<fieldset><legend>Food Items</legend>\n";
	$html .= "<label>Separate Items with a Return</label><textarea id=\"foodItemsList\" name=\"foodItemsList\" rows=\"5\" cols=\"20\"></textarea>\n";
	$html .= "</fieldset>";
	}
	

if (isset($_POST['writeDays'])){
	$html .= "<fieldset><legend>Write Days</legend>\n";
	$html .= "<label>Select Available Promotion Days</label>\n";
	$html .= "<select name=\"writeDays[]\" multiple=\"multiple\">
			<option selected=\"selected\">Monday</option>
			<option selected=\"selected\">Tuesday</option>
			<option selected=\"selected\">Wednesday</option>
			<option selected=\"selected\">Thursday</option>
			<option selected=\"selected\">Friday</option>
			<option>Saturday</option>
			<option>Sunday</option>
			</select>\n";
	$html .= "</fieldset>";
}

if (isset($_POST['writeOwnWords'])){
	$html .= "<fieldset><legend>Write Own Words</legend>\n";
	$html .= "<p>Write Own Words is enabled</p>\n";
	$html .= "</fieldset>";
	$html .= "<input type=\"hidden\" name=\"writeOwnWords\"/>\n";
}

if (isset($_POST['addOwnPrice'])){
	$html .= "<fieldset><legend>Add Own Price</legend>\n";
	$html .= "<p>Add Own Price is enabled</p>\n";
	$html .= "</fieldset>";
	$html .= "<input type=\"hidden\" name=\"addOwnPrice\"/>\n";
}

if (isset($_POST['pricePoint'])){
	$html .= "<fieldset><legend>Add Price Point</legend>\n";
	//$startPrice, $endPrice, $interval
	$html .= "<label for=\"pricePointVal1\">Enter Start Amount</label><input type=\"text\" id=\"pricePointVal1\" name=\"pricePointVal1\" size=\"6\" /><br/>\n";
	$html .= "<label for=\"pricePointVal2\">Enter End Amount</label><input type=\"text\" id=\"pricePointVal2\" name=\"pricePointVal2\" size=\"6\" /><br/>\n";
	$html .= "<label for=\"pricePointVal3\">Enter Price Interval</label><input type=\"text\" id=\"pricePointVal3\" name=\"pricePointVal3\" size=\"6\" /><br/>\n";
	$html .= "</fieldset>";
	$html .= "<input type=\"hidden\" name=\"pricePoint\"/>\n";
}

if (isset($_POST['writeTimes'])){
	$html .= "<fieldset><legend>Add Own Times</legend>\n";
	//$startHour, $startMin, $endHour, $endMin, $interval
	$html .= "<label for=\"writeTimesVal1\">Enter Start Hour</label><input type=\"text\" id=\"writeTimesVal1\" name=\"writeTimesVal1\" size=\"6\" value=\"09\"/><br/>\n";
	$html .= "<label for=\"writeTimesVal2\">Enter Start Minute</label><input type=\"text\" id=\"writeTimesVal2\" name=\"writeTimesVal2\" size=\"6\" value=\"30\" /><br/>\n";
	$html .= "<label for=\"writeTimesVal3\">Enter End Hour</label><input type=\"text\" id=\"writeTimesVal3\" name=\"writeTimesVal3\" size=\"6\" value=\"11\" /><br/>\n";
	$html .= "<label for=\"writeTimesVal4\">Enter End Minute</label><input type=\"text\" id=\"writeTimesVal4\" name=\"writeTimesVal4\" size=\"6\" value=\"30\" /><br/>\n";
	$html .= "<label for=\"writeTimesVal5\">Enter Interval in Minutes</label><input type=\"text\" id=\"writeTimesVal5\" name=\"writeTimesVal5\" size=\"6\" value=\"10\" /><br/>\n";
	$html .= "</fieldset>";
	$html .= "<input type=\"hidden\" name=\"writeTimes\"/>\n";
	
	}

if (isset($_POST['discountTable'])){
	$html .= "<fieldset><legend>Add Discount Table File</legend>\n";
	$html .= "<p></p>\n";
	$html .= "<input type=file name=\"discountTable\" class=\"bginput\">\n";
	$html .= "</fieldset>";
}

if (isset($_POST['addLogo'])){
	$html .= "<fieldset><legend>Add Own Logo</legend>\n";
	$html .= "<p>Add Own Logo is enabled</p>\n";
	$html .= "</fieldset>";
	$html .= "<input type=\"hidden\" name=\"addLogo\"/>\n";
}


if (isset($_POST['addOffer'])){
	$html .= "<fieldset><legend>Add Own Offer</legend>\n";
	$html .= "<p>Add Own Offer is enabled</p>\n";
	$html .= "</fieldset>";
	$html .= "<input type=\"hidden\" name=\"addOffer\"/>\n";
}




$html .= "<input type=\"submit\" value='Add Promotion' />\n";

$html .= "</form>\n";

$html .= "</div>\n";

?>

<?php
// ----------------------------------------------
//  Instantiate generic display class
// ----------------------------------------------    
 	require_once 'display/genericdisplay.class.php';
    $GENDISPLAY = new Genericdisplay();
    echo ($GENDISPLAY->html_header('admin_style.css'));
    
    // OPEN THE BODY FOR OUTPUT
	$GENDISPLAY->body_open();
	
// ----------------------------------------------
//  Instantiate admin display class
// ----------------------------------------------    
	require_once 'display/admindisplay.class.php';
	$ADMDISPLAY = new Admindisplay();
    

    ?>
    
    <div id="maincontainer">

<?php echo ($ADMDISPLAY->display_header()); ?>
<div id="contentwrapper">
<div id="contentcolumn">
<div class="innertube">
<?php
echo ($html);
?>


</div>
</div>
</div>

<div id="leftcolumn">
<div class="innertube">
<?php

 echo ($ADMDISPLAY->display_main_admin_nav());

?>
</div>
</div>

<div id="footer"><p>&copy; Caramba Marketing</p></div>

</div>

<?php
$promotion = $SESS->saveObject($promotion);
$result = $SESS->saveSession("Promotion", $promotion);
if (!$result) {
	echo ("ERROR SAVING SESSION");
	}
$GENDISPLAY->body_close();
$GENDISPLAY->html_close();
?>

